package mn.slide.laborberlin.adaptor;

import java.util.List;

import mn.slide.laborberlin.main.AnsprechparterDetialActivity;
import mn.slide.laborberlin.main.AnsprechpartnerSubActivity;
import mn.slide.laborberlin.main.R;
import mn.slide.laborberlin.model.PListMainModel;
import mn.slide.laborberlin.tabgroup.GroupStarter;
import mn.slide.laborberlin.tabgroup.HomeGroupActivity;
import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AnspachAdapter extends ArrayAdapter<PListMainModel> {
	private final List<PListMainModel> list;
	private final Activity context;
	private LayoutInflater inflate;

	public AnspachAdapter(Activity context, List<PListMainModel> list) {
		super(context, R.layout.list_item_middle, list);
		this.list = list;
		this.context = context;
		inflate = context.getLayoutInflater();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = convertView;
		final ViewHolder child;
		final PListMainModel model = list.get(position);
		if (rowView == null) {
			rowView = inflate.inflate(R.layout.list_item_middle, null, true);
			child = new ViewHolder();
			child.title = (TextView) rowView.findViewById(R.id.title);
			child.subTitle = (TextView) rowView.findViewById(R.id.subtitle);
			child.arrow = (ImageView) rowView.findViewById(R.id.chevron);
			rowView.setTag(child);
		} else {
			child = (ViewHolder) rowView.getTag();
		}
		rowView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (model.getChild().size() == 1) {
					Intent intent = new Intent(context,
							AnsprechparterDetialActivity.class);
					intent.putExtra("model", model.getChild().get(0).getChild());
					HomeGroupActivity.group.goTo(intent);
				} else {
					Intent intent = new Intent(context,
							AnsprechpartnerSubActivity.class);
					intent.putParcelableArrayListExtra("mainList",
							model.getChild());
					intent.putExtra("title", model.getValue());

					GroupStarter.getInstance().startActivity(context, intent);
				}
			}
		});
		child.title.setText(model.getValue());
		child.arrow.setVisibility(View.VISIBLE);
		child.subTitle.setVisibility(View.GONE);

		return rowView;
	}

	static class ViewHolder {
		protected TextView title;
		protected TextView subTitle;
		protected ImageView arrow;
	}
}
